package com.mofang.powerdekorhelper.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.FileProvider;

import com.mofang.powerdekorhelper.model.ResultMessage;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.File;

/**
 * Created by luofei on 2017/7/13 0013.
 */

public class ShareUtils {

    private static final int THUMB_SIZE = 150;

    private Context mContext;

    String shareTitle = "";

    private IWXAPI api;

    public ShareUtils(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 分享信息到朋友圈
     *
     * @param context
     * @param file
     */
    public void shareToTimeLine(Context context, File file) {

        Intent intent = new Intent();
        ComponentName comp = new ComponentName("com.tencent.mm",
                "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        intent.setComponent(comp);
        intent.setAction("android.intent.action.SEND");
        intent.setType("image/*");
        Uri uri = FileProvider.getUriForFile(context, "com.mofang.powerdekorhelper.provider", file);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(intent);
    }

    // 分享小程序
    public void shareMinProgram(String title, String description, Integer shareType, Integer id, Bitmap bmp) {

        api = WXAPIFactory.createWXAPI(mContext, "wx369d7a06ba476b22");

        WXMiniProgramObject miniProgram = new WXMiniProgramObject();
        miniProgram.webpageUrl = "http://www.qq.com";
        miniProgram.userName = "gh_d7bd9b23a736";
        miniProgram.path = "pages/activity/activity_details/activity_details?type=" + shareType + "&value=" + id;
        WXMediaMessage msg = new WXMediaMessage(miniProgram);
        if (description == null || description.equals("")) {
            msg.title = title;
        } else {
            msg.title = description;
        }
        //msg.title = title;
        msg.description = description;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);
        msg.thumbData = ImageUtils.bmpToByteArray(thumbBmp, true);
        bmp.recycle();

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        Bundle bundle = new Bundle();
        bundle.putInt("value", 1);
        req.toBundle(bundle);

        api.sendReq(req);

        if (shareType == 1) {
            shareAddIntegeral(shareType, title, id);
        }
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    // 加积分
    private void shareAddIntegeral(Integer shareType, String title, Integer activityId) {

        int point = 1;

        String sharedType = "分享";

        int userId = SharePerforenceUtils.getInstance(mContext).getUserID();

        new SharePerenter().getSharePoint(userId, shareType, sharedType, point, title, activityId, new SharedCallBack());
    }

   /* // 分享本地图片
    public void shareLocalImage(Activity activity, SHARE_MEDIA shareMedia, File file, String title) {
        if (file != null) {

            this.shareTitle = title;

            UMImage umImage = new UMImage(activity, file);

            umImage.compressStyle = UMImage.CompressStyle.SCALE;//大小压缩，默认为大小压缩，适合普通很大的图

            umImage.compressFormat = Bitmap.CompressFormat.PNG;//用户分享透明背景的图片可以设置这种方式，但是qq好友，微信朋友圈，不支持透明背景图片，会变成黑色

            new ShareAction(activity)
                    .setPlatform(shareMedia)
                    .withText("圣象集客")
                    .setCallback(myUMShareListener)
                    .withMedia(umImage)
                    .share();
        }
    }

    // 分享图片
    public void shareImage(Activity activity, SHARE_MEDIA shareMedia, String imageUrl) {

        UMImage umImage = new UMImage(activity, imageUrl);

        new ShareAction(activity)
                .setPlatform(shareMedia)
                .setCallback(myUMShareListener)
                .withMedia(umImage)
                .share();
    }

    // 图片类型分享
    public void shareWChatImage(Bitmap bmp) {

        api = WXAPIFactory.createWXAPI(mContext, "wxb7297be1a65b0fc3");

        WXImageObject imgObject = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObject;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);
        bmp.recycle();
        msg.thumbData = ImageUtils.bmpToByteArray(thumbBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        api.sendReq(req);
    }

    // 分享链接
    public void shareLink(Activity activity, SHARE_MEDIA shareMedia, String imageUrl, String title, String content, String targetUrl) {

        this.shareTitle = title;

        UMImage umImage = new UMImage(activity, imageUrl);

        UMWeb web = new UMWeb(targetUrl);
        web.setTitle(title);
        web.setDescription(content);
        web.setThumb(umImage);

        new ShareAction(activity)
                .setPlatform(shareMedia)
                .withMedia(web)
                .setCallback(myUMShareListener)
                .share();
    }*/

  /*  // 分享回调监听
    private UMShareListener myUMShareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            Toast.makeText(mContext, " 分享成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(mContext, " 分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(mContext, " 分享取消", Toast.LENGTH_SHORT).show();
        }
    };*/

    class SharedCallBack implements SharePerenter.ISharedCallBack {

        @Override
        public void onSuccess(ResultMessage resultMessage2) {

            if (resultMessage2.getCode().equals("0")) {
                //加积分成功
                //Toast.makeText(mContext, " 已为您加积分", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError(String errorInfo) {
            //加积分失败
            //Toast.makeText(mContext, " 加积分失败", Toast.LENGTH_SHORT).show();
        }
    }
}
